defmodule RotationalCipher do
  @doc """
  Given a plaintext and amount to shift by, return a rotated string.

  Example:
  iex> RotationalCipher.rotate("Attack at dawn", 13)
  "Nggnpx ng qnja"
  """
  #require Logger
  #alias Logger, as: LOG

  @spec rotate(text :: String.t(), shift :: integer) :: String.t()
  def rotate(text, shift) do
    #text
    #|> to_charlist
    ##|> Enum.map(&transform(&1, shift))
    #|> Enum.map(fn(&1) -> cipher(&1, shift) end)
    #|> List.to_string

    code = Enum.map(to_charlist(text), 
        fn(c) -> cipherr(c, shift) end)

    List.to_string code
  end

  def cipherr(c, shift) do
    #IO.inspect c
    #LOG.debug("got #{c} by #{shift}")
    cond do
      c in ?a..?z -> ?a+rem(c - ?a + shift, 26)
      c in ?A..?Z -> ?A+rem(c - ?A + shift, 26)
      !(c in ?A..?Z) && !(c in ?a..?z)-> c
    end
  end


end
